﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RipPoll.Web.Mvc.Models;
using RipPoll.Library;
using RipPoll.Common;

namespace RipPoll.Web.Mvc.Controllers
{
    public class CommunityController : BaseController
    {
        //
        // GET: /Poll/


        public ActionResult Preview()
        {
            if (!IsUserLoggedIn())
                return RedirectToAction("signin", "user");

            PollPE pe = Session[WebConstants.UnSavedPoll] as PollPE;
            PollModel pm = new PollModel();
            pm.Id = pe.Id;
            pm.Name = pe.Name;
            if (pe.Category.Id == 1)
                pm.Category = "Yes/No";
            else
                pm.Category = "Multiple Choice";
            pm.Choices = (from c in pe.Choices
                         select c.Choice).ToList<string>();

            return View(pm);
        }

        public ActionResult MyCommunities()
        {
            UserPE upe = Session[WebConstants.CurrentUser] as UserPE;
            if (upe != null)
            {
                ViewBag.City = upe.City;
                ViewBag.State = upe.State;

                CommunityServices.CommunityServicesClient cclient = new CommunityServices.CommunityServicesClient();
                List<CommunityPE> clist = cclient.GetUserCommunities(upe.Id);

                return View(clist);
            }
            return View();
        }
        
        
      
        public ActionResult Create1()
        {
            if (!IsUserLoggedIn())
                return RedirectToAction("signin", "user");

            UserPE upe = Session[WebConstants.CurrentUser] as UserPE;
            ViewBag.UserCity = upe.City;
            ViewBag.FirstName = upe.FirstName;
            ViewBag.LastName = upe.LastName;
            ViewBag.State = upe.State;
            CommunityPE cpe = Session[WebConstants.UnSavedCommunity] as CommunityPE;
            if (cpe != null)
                ViewBag.CommunityText = cpe.Name;
            if (string.IsNullOrEmpty(ViewBag.CommunityText))
                ViewBag.CommunityText = "Type in your community here. 25 characters max.";
            return View(cpe);
        }


        [HttpPost()]
        public ActionResult Create1(string returnUrl)
        {
            if (!IsUserLoggedIn())
                return RedirectToAction("signin", "user");

            string question = Request.Form["question"];
            string firstname = Request.Form["firstname"];
            string lastname = Request.Form["lastname"];
            string city = Request.Form["city"];
            string state = Request.Form["state"];

            UserPE upe = Session[WebConstants.CurrentUser] as UserPE;
            if (!string.IsNullOrEmpty(city))
                upe.City = city;
            if (!string.IsNullOrEmpty(firstname))
                upe.FirstName = firstname;
            if (!string.IsNullOrEmpty(lastname))
            upe.LastName = lastname;
            if (!string.IsNullOrEmpty(state))
            upe.State = state;

            Session[WebConstants.CurrentUser] = upe;

            UserServices.UserServicesClient ucli = new UserServices.UserServicesClient();
            ucli.UpdateUserForPoll(upe);

            CommunityPE commPE = new CommunityPE();
            commPE.Name = question;
            Session[WebConstants.UnSavedCommunity] = commPE;

            return RedirectToAction("Create2", "Community");

        }

        public ActionResult create2()
        {
            if (!IsUserLoggedIn())
                return RedirectToAction("signin", "user");

            CommunityPE cpe = Session[WebConstants.UnSavedCommunity] as CommunityPE;
            return View(cpe);
        }


        [HttpPost]
        public ActionResult create2(HttpPostedFileBase file)
        {
            if (!IsUserLoggedIn())
                return RedirectToAction("signin", "user");

            CommunityPE cpe = Session[WebConstants.UnSavedCommunity] as CommunityPE;
            if (cpe == null)
                return RedirectToAction("create1", "Community");

            if (file != null && file.ContentLength > 0)
            {
                var fileName = System.IO.Path.GetFileName(file.FileName);
                var path = System.IO.Path.Combine(Server.MapPath("../content/images/uploads"), fileName);
                file.SaveAs(path);


                cpe.ImagePath = path;
                Session[WebConstants.UnSavedCommunity] = cpe;
            }

            return RedirectToAction("Create3", "Community");


        }


        public ActionResult Create3()
        {
            CommunityPE cpe = Session[WebConstants.UnSavedCommunity] as CommunityPE;
            return View(cpe);
        }


        [HttpPost()]
        public ActionResult Create3(string returnUrl)
        {
            if (!IsUserLoggedIn())
                return RedirectToAction("signin", "user");

            string commChoice = Request.Form["communitychoice"];
            
            CommunityPE cpe = Session[WebConstants.UnSavedCommunity] as CommunityPE;
            if (cpe == null)
                return RedirectToAction("create1", "Community");

            cpe.AccesslevelId = int.Parse(commChoice);
            Session[WebConstants.UnSavedCommunity] = cpe;

            return RedirectToAction("Create4", "community");
        }

        public ActionResult Create4()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Save()
        {
            if (!IsUserLoggedIn())
                return RedirectToAction("signin", "user");
            string question = Request.Form["question"];

            CommunityPE cpe = Session[WebConstants.UnSavedCommunity] as CommunityPE;
            if (cpe == null)
                return RedirectToAction("create1", "Community");

            cpe.Question = question;
            CommunityServices.CommunityServicesClient commCli = new CommunityServices.CommunityServicesClient();
            UserPE upe = Session[WebConstants.CurrentUser] as UserPE;
            commCli.AddCommunity(cpe, upe);

            Session[WebConstants.UnSavedCommunity] = null;
            Session[WebConstants.SavedCommunity] = cpe;

            return RedirectToAction("mycommunities", "community");
        }

    }
}
